/*
 * @Author: Even
 * @Date: 2024-02-01 11:00:53
 * @LastEditors: Even
 * @LastEditTime: 2024-03-17 20:32:25
 * @FilePath: \toolhead-triple\Apps\wrapper.cpp
 * @Description: 此处管理APP层所有功能函数调用，为main.c/freeRTOS.c提供API
 */
#include "wrapper.h"
#include "../Bsp/bsp_led/bsp_led.h"
#include "../Modules/can_host/can_host.h"
#include "FreeRTOS.h"
#include "can_host.h"
#include "cmsis_os.h"
#include "cmsis_os2.h"
#include "projdefs.h"
#include "task.h"
#include <stdint.h>

osThreadId_t LedTaskHandle;
const osThreadAttr_t LedTask_attributes = {.name = "LedTask",
                                           .stack_size = 128 * 4,
                                           .priority =
                                               (osPriority_t)osPriorityLow};

osThreadId_t CanEventTaskHandle;
const osThreadAttr_t CanEventTask_attributes = {
    .name = "CanEventTask",
    .stack_size = 128 * 4,
    .priority = (osPriority_t)osPriorityHigh,
};

osThreadId_t CanReceiveTaskHandle;
const osThreadAttr_t CanReceiveTask_attributes = {
    .name = "CanReceiveTask",
    .stack_size = 128 * 4,
    .priority = (osPriority_t)osPriorityHigh,
};

osThreadId_t MonitorTaskHandle;
const osThreadAttr_t MonitorTask_attributes = {
    .name = "MonitorTask",
    .stack_size = 128,
    .priority = (osPriority_t)osPriorityLow,
};

osThreadId_t HeartBeatTaskHandle;
const osThreadAttr_t HeartBeatTask_attributes = {
    .name = "HeartBeatTask",
    .stack_size = 128 * 2,
    .priority = (osPriority_t)osPriorityLow,
};

uint32_t stack_remain;
uint32_t count = 0;

static void LedTask(void *argument) { led.LedHandler(argument); }

static void CanEventTask(void *argument) { canhost.EventHandler(argument); }

static void CanReceiveTask(void *argument) { canhost.ReceiveHandler(argument); }

static void MonitorTask(void *argument) {
  while (1) {
    stack_remain = osThreadGetStackSpace(CanEventTaskHandle);
    osDelay(100);
  }
}

static void HeartBeatTask(void *argument) {
  // SSTP_Event_t   event = {EID_SYS_CTRL_ACK, SYSCTL_OPC_GET_STATUES};

  int counter = 0;

  while (1) {
// do following every 10ms without being blocked
#if HAS_FILAMENT_SENSOR
    runout.run();
#endif

    // systemservice.CheckException();

    if (++counter > 1) { // 100
      counter = 0;

      // do following every 1s
      // upgrade.Check();
      canhost.SendHeartbeat();
      count++;
      // stack_remain = osThreadGetStackSpace(CanEventTaskHandle);
    }

    // CAN_TxHeaderTypeDef TxMesHeader;
    // TxMesHeader.StdId = 0x12;
    // TxMesHeader.DLC = 8;
    // TxMesHeader.ExtId = 0x12;
    // TxMesHeader.IDE = CAN_ID_STD;
    // TxMesHeader.RTR = CAN_RTR_DATA;
    // uint8_t pData[] = {0, 1, 2, 3, 4, 5, 6, 7};
    // HAL_CAN_AddTxMessage(&hcan1, &TxMesHeader, pData,
    //                      (uint32_t *)CAN_TX_MAILBOX0);
    
    // CAN_TxHeaderTypeDef txHeader;
    // uint8_t txData[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
    // uint32_t txMailbox = 0;

    // txHeader.StdId = 0x123; 
    // txHeader.RTR = CAN_RTR_DATA;
    // txHeader.IDE = CAN_ID_STD;
    // txHeader.DLC = 8;

    // HAL_CAN_AddTxMessage(&hcan1, &txHeader, txData,&txMailbox);
    // sleep for 10ms
    // vTaskDelay(pdMS_TO_TICKS(500));
    // osDelay(pdMS_TO_TICKS(10)); 
    osDelay(50);
  }
}

void Setup(void *argument) {
  // Hardware Init
  canhost.Init();
  // Create Tasks
  LedTaskHandle = osThreadNew(LedTask, NULL, &LedTask_attributes);
  CanEventTaskHandle =
      osThreadNew(CanEventTask, NULL, &CanEventTask_attributes);
  MonitorTaskHandle = osThreadNew(MonitorTask, NULL, &MonitorTask_attributes);
  // CanReceiveTaskHandle =
  //     osThreadNew(CanReceiveTask, NULL, &CanReceiveTask_attributes);

  // TO TEST CAN Transmit,waiting modules(slave board)...
  HeartBeatTaskHandle =
      osThreadNew(HeartBeatTask, NULL, &HeartBeatTask_attributes);
  osThreadExit();
}